package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhousb.deviceManager.mapper.KiAssetMapper;
import com.zhousb.deviceManager.mapper.KiFloorMapper;
import com.zhousb.deviceManager.mapper.KiPositionMapper;
import com.zhousb.deviceManager.mapper.KiTransferRecordMapper;
import com.zhousb.deviceManager.model.bo.TransferSubmitBo;
import com.zhousb.deviceManager.model.entity.KiAsset;
import com.zhousb.deviceManager.model.entity.KiFloor;
import com.zhousb.deviceManager.model.entity.KiPosition;
import com.zhousb.deviceManager.model.entity.KiTransferRecord;
import com.zhousb.deviceManager.model.so.TransferRecordSo;
import com.zhousb.deviceManager.model.vo.TransferRecordVo;
import com.zhousb.deviceManager.security.JwtUser;
import com.zhousb.deviceManager.service.TransferRecordService;
import com.zhousb.deviceManager.utils.AuthUtils;
import com.zhousb.deviceManager.utils.IdGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhousb
 * @date 2024/11/1 14:53
 * @description
 */
@Service
public class TransferRecordServiceImpl implements TransferRecordService {

    @Resource
    KiTransferRecordMapper kiTransferRecordMapper;
    @Resource
    private KiAssetMapper kiAssetMapper;
    @Resource
    private KiFloorMapper kiFloorMapper;
    @Resource
    private KiPositionMapper kiPositionMapper;
    @Resource
    private AuthUtils authUtils;

    @Override
    public IPage<TransferRecordVo> page(Page<?> page, TransferRecordSo so) {
        return kiTransferRecordMapper.page(page, so);
    }

    @Override
    public boolean submit(TransferSubmitBo bo) {
        if (bo.getAssets() == null || bo.getAssets().isEmpty()) {
            return true;
        }

        JwtUser jwtUser =authUtils.getUser();

        List<KiTransferRecord> records = new ArrayList<>();
        List<KiAsset> assets = kiAssetMapper.selectBatchIds(bo.getAssets());
        for (KiAsset asset : assets) {
            KiTransferRecord record = new KiTransferRecord();
            record.setId(IdGenerator.nextId());
            record.setAssetId(asset.getId());
            //todo 楼层和库房调整为存id
            record.setOriginalFloor("");
            record.setOriginalPosition("");
            record.setOriginalAddress(asset.getAddress());
            records.add(record);
        }

        LambdaUpdateWrapper<KiAsset> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(KiAsset::getId, bo.getAssets());
        KiFloor assetFloor = kiFloorMapper.selectById(bo.getFloorId());
        String targetFloor = assetFloor == null ? null : assetFloor.getFloor();
        updateWrapper.set(KiAsset::getAssetFloor, targetFloor);
        KiPosition assetPosition = kiPositionMapper.selectById(bo.getPositionId());
        String targetPosition = assetPosition == null ? null : assetPosition.getPosition();
        updateWrapper.set(KiAsset::getAssetPosition, targetPosition);
        if (StringUtils.isNotBlank(bo.getAddress())) {
            updateWrapper.set(KiAsset::getAddress, bo.getAddress());
        }
        kiAssetMapper.update(updateWrapper);

        kiTransferRecordMapper.batchInsert(records,targetFloor,targetPosition,bo.getAddress(),bo.getReason(),jwtUser.getUserId());
        return true;
    }
}
